package util

import (
	"github.com/Unknwon/goconfig"
	"log"
)

var cfg *goconfig.ConfigFile
var errCfg error

func ConfigInit(filepath string) {
	cfg, errCfg = goconfig.LoadConfigFile(filepath)
	if errCfg != nil {
		log.Fatal(errCfg)
	}
}

func GetDatabase(db string) (source string) {
	user, _ := cfg.GetValue(db, "user")
	password, _ := cfg.GetValue(db, "password")
	host, _ := cfg.GetValue(db, "host")
	port, _ := cfg.GetValue(db, "port")
	database, _ := cfg.GetValue(db, "db")
	charset, _ := cfg.GetValue(db, "charset")
	source = user + ":" + password + "@tcp(" + host + ":" + port + ")/" + database + "?charset=" + charset
	return source
}

func GetSystemValue(key string) string {
	if value, err := cfg.GetValue("system", key); err == nil {
		return value
	}
	return ""
}

func GetSettingValue(key string) string {
	if value, err := cfg.GetValue("setting", key); err == nil {
		return value
	}
	return ""
}
